// const { length } = require("../../../mybehaviors/location/location");

// packageI/warehouseFarm/warehouseFarmIndex/warehouseFarmIndex.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iconOption: [
      {
        icon: `${app.globalData.imgPath}warehouse-farm/cabbage.png`,
        name: "菜",
        width: "256rpx",
        bottom: "420rpx",
        left: "32rpx",
        right: "auto",
        text: "bottom",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/eggs.png`,
        name: "肉蛋",
        width: "216rpx",
        bottom: "508rpx",
        right: "60rpx",
        left: "auto",
        text: "top",
        textTop: "0rem",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/well.png`,
        name: "水",
        width: "132rpx",
        bottom: "632rpx",
        right: "208rpx",
        left: "auto",
        text: "top",
        textTop: "-65rpx",
        zIndex: 1,
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/milk.png`,
        name: "牛奶",
        width: "316rpx",
        bottom: "684rpx",
        left: "0",
        right: "auto",
        text: "bottom",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/seasoning.png`,
        name: "调料",
        width: "168rpx",
        bottom: "284rpx",
        left: "28rpx",
        right: "auto",
        text: "bottom",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/Rice.png`,
        name: "大米",
        width: "160rpx",
        bottom: "544rpx",
        left: "0",
        right: "auto",
        text: "bottom",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/flour.png`,
        name: "面粉",
        width: "160rpx",
        bottom: "360rpx",
        right: "60rpx",
        left: "auto",
        text: "bottom",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/liquor.png`,
        name: "酒",
        width: "124rpx",
        bottom: "248rpx",
        left: "188rpx",
        right: "auto",
        text: "bottom",
      },
      {
        icon: `${app.globalData.imgPath}warehouse-farm/delivery.png`,
        name: "快递仓",
        width: "176rpx",
        bottom: "796rpx",
        left: "368rpx",
        right: "auto",
        text: "top",
        textTop: "32rpx",
      },
    ],
    showAnnouncement: false, //广告弹窗
    showDynamic: false, //动态弹窗
    showRank: false, //排行榜弹窗
    showReward: false, //红包弹窗
    info: {},
    gift: {}, //是否有红包信息
    markid: null,
    // 排行榜数据
    rank_loading: false,
    rank_finished: false,
    rank_refreshing: false,
    rank_page: 1,
    rank_list: [],
    rank_self: {},
    //动态数据
    dynamic_loading: false,
    dynamic_finished: false,
    dynamic_refreshing: false,
    dynamic_page: 1,
    dynamic_list: [],
    dynamic_self: {},

    todayDynamic: [], //当天动态
    beforeDynamic: [], //以前的动态信息

    shareInfo: {}, //分享信息
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.gapid) {
      this.setData({
        markid: options.gapid,
      });
    }
  },

  getData() {
    let _json = {};
    if (this.data.markid) {
      _json.gift_key = this.data.markid;
    }

    let urlStr = app.getNetAddresss("plugin.warehouse.frontend.controllers.center.index");
    app._postNetWork({
      url: urlStr,
      data: _json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data,
            gift: res.data.gift || {},
            showReward: res.data.gift && res.data.gift.judge ? true : false,
            shareInfo: res.data.share_info,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  getPopupData(_tag) {
    let _url = "plugin.warehouse.frontend.controllers.center.rank";
    if (_tag == "dynamic_") {
      _url = "plugin.warehouse.frontend.controllers.center.dynamic-state";
    }
    this.data[`${_tag}loading`] = true;
    let urlStr = app.getNetAddresss(_url);
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data[`${_tag}page`],
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let _list = this.data[`${_tag}list`];
          this.setData({
            [_tag + "self"]: res.data.self,
            [_tag + "refreshing"]: false,
          });
          this.data[`${_tag}page`] = this.data[`${_tag}page`] + 1;
          this.data[`${_tag}loading`] = false;
          if (res.data.list.current_page == res.data.list.last_page) {
            this[`${_tag}finished`] = true;
          }

          if (_tag == "dynamic_") {
            if (this.data[`${_tag}page`] == 1) {
              this.data.todayDynamic = [];
              this.data.beforeDynamic = [];
            }
            let _datalist = res.data.list.data;
            _datalist.forEach((item) => {
              if (this.isToday(item.created_at)) {
                this.data.todayDynamic.push(item);
              } else {
                this.data.beforeDynamic.push(item);
              }
            });
            this.setData({
              todayDynamic: this.data.todayDynamic,
              beforeDynamic: this.data.beforeDynamic,
            });
          } else {
            this.setData({
              [_tag + "list"]: _list.concat(res.data.list.data),
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  cloneReward() {
    this.setData({
      showReward: false,
    });
    wx.reLaunch({ url: "/packageI/warehouseFarm/warehouseFarmIndex/warehouseFarmIndex" });
  },
  isToday(date) {
    //是否为当天数据
    return new Date(date).toDateString() === new Date().toDateString();
  },
  RefreshList(e) {
    let _tag = e.target.dataset.tag;
    this.data[`${_tag}loading`] = true;
    this.data[`${_tag}page`] = 1;
    this.data[`${_tag}list`] = [];
    this.data[`${_tag}finished`] = false;
    this.data.todayDynamic = [];
    this.data.beforeDynamic = [];
    this.getPopupData(_tag);
  },
  LoadList(e) {
    let _tag = e.target.dataset.tag;
    if (this.data[`${_tag}finished`] || this.data[`${_tag}loading`]) {
      return;
    }
    this.getPopupData(_tag);
  },
  openPopup(e) {
    let _params = e.currentTarget.dataset;
    if (_params.tag) {
      let _tag = _params.tag;
      if (_tag == "showDynamic") {
        this.getPopupData("dynamic_");
      } else if (_tag == "showRank") {
        this.getPopupData("rank_");
      }
      this.setData({
        [_tag]: _params.set,
      });
    }
  },

  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = "";
    const { url, ...otherParam } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== "") _Param += key + "=" + otherParam[key] + "&";
    });
    if (url === "") return;
    wx.navigateTo({
      url: url + "?" + _Param.slice(0, -1),
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      showReward: false,
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.shareInfo.title,
      imageUrl: this.data.shareInfo.thumb,
    };
  },
});
